package com.zhiyou100.api.java8_date;

import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;

public class Demo2 {

    public static void main(String[] args) {
        test();

    }

    /**
     * LocalTime以及LocalDateTime的演示自行书写
     */
    public static void test(){
        LocalDate localDate = LocalDate.of(2020, 10, 01);
        //增加年份  plus
        localDate = localDate.plusYears(1);
        System.out.println("localDate = " + localDate);
        //月份增加  plus
        localDate = localDate.plusMonths(1);
        localDate = localDate.plusDays(10);  //日期增加
        System.out.println("localDate = " + localDate);
        //减少   minus
        localDate = localDate.minusMonths(2);
        System.out.println("localDate = " + localDate);  //2021-09-11
        //根据字段增加或减少
        localDate = localDate.plus(1, ChronoUnit.YEARS);  //2022-9-11
        System.out.println("localDate = " + localDate);
        localDate = localDate.minus(1,ChronoUnit.MONTHS);  //2022-08-11
        System.out.println("localDate = " + localDate);
        //获取两个日期之间的间隔几年几月几日
        Period period = localDate.until(LocalDate.of(2022, 1, 18));
        int years = period.getYears();
        System.out.println("years = " + years);
    }

    //修改localDate日期
    public static void editLocalDate() {
        //获取当前日期
        LocalDate localDate = LocalDate.now();
        //将当前年修改为2020
        localDate = localDate.withYear(2020);
        System.out.println("localDate = " + localDate);
        //修改月份
        localDate = localDate.withMonth(10);
        //修改日期
        localDate = localDate.withDayOfMonth(30);
        System.out.println("localDate = " + localDate);
        //根据某个字段修改日期   YEAR  MONTH_OF_YEAR  DAY_OF_MONTH  DAY_OF_WEEK  DAY_OF_YEAR
        localDate = localDate.with(ChronoField.MONTH_OF_YEAR, 11);
        System.out.println("localDate = " + localDate);
    }


}
